package gokyesil.game2d;

import gokyesil.game2d.actors.BlockSaveImage;
import gokyesil.game2d.actors.MapSaveImage;
import gokyesil.game2d.renderer.Assets;
import gokyesil.game2d.renderer.Renderer;
import gokyesil.game2d.screens.LoadingScreen;
import gokyesil.game2d.screens.MainScreen;
import gokyesil.game2d.screens.MapEditor;

import java.io.File;

import javax.swing.JFileChooser;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Json;
/**
 * The Main Class that controls and switches the screens
 * @author Alper Kamil BOZKURT
 *
 */
public class Game2D extends Game
{
	/**
	 * The Width of the game world.
	 */
	public static final int WIDTH = 1000;
	/**
	 * The height of the game world.
	 */
	public static final int HEIGHT = 600;
	/**
	 * Loading Screen of the game.
	 */
	private LoadingScreen loadingScreen;
	/**
	 * Main Screen of the game.
	 */
	private MainScreen mainScreen;
	/**
	 * Map Editor Screen of the game.
	 */
	private MapEditor mapEditor;
	/**
	 * Responsible for printing images to the screens.
	 */
	private Renderer gameRenderer;
	/**
	 * Called when the game starts.
	 * The method initializes the screens and set mainScreen as the current screen.
	 */
	public void create() 
	{	
		//gameRenderer = new Renderer();
		Assets.getBackground();
		loadingScreen = new LoadingScreen();
		loadingScreen.render(0);
		
		Assets.loadTextures();
		
		mainScreen = new MainScreen();
		mapEditor = new MapEditor();

		this.setScreen(mainScreen);
		//this.setScreen(currentScreen);
	}
	/**
	 * Called normally 60 times per second during the game.
	 * The method clears the screen and does the necessary actions when the player presses a button.
	 */
	public void render()
	{
		Gdx.graphics.getGL20().glClearColor( 0.999f, 0.999f, 0.999f, 1 );
		Gdx.graphics.getGL20().glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );

		super.render();
		
		// When the current screen is the mainScreen
		if(this.getScreen() == mainScreen)
		{
			if(mainScreen.newGame.isPressed())
			{
				
			}
			// When the map editor button is pressed, sets map editor screen as the current screen.
			else if(mainScreen.mapEditor.isPressed())
			{
				this.setScreen(mapEditor);
			}
			// When the load game button is pressed, opens the dialog and loads the file chosen
		/*	else if(mainScreen.loadGame.isPressed())
			{
				MapSaveImage saveImage = null;
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(fc);
	            if (returnVal == JFileChooser.APPROVE_OPTION) 
	            {
	                File file = fc.getSelectedFile();
	                String filePath = file.getPath();
					try
					{ 
						FileHandle fileHandle = Gdx.files.local(filePath);
						Json json = new Json();
						//json.setOutputType(OutputType.minimal);
						json.setElementType(MapSaveImage.class, "blockList", BlockSaveImage.class);
						json.setElementType(MapSaveImage.class, "map", boolean[].class);
						saveImage =json.fromJson(MapSaveImage.class, fileHandle);
					}
					catch(Exception exc)
					{
						exc.printStackTrace(); 
					}
	            }
	            if(saveImage != null)
	            	mapEditor.load((MapSaveImage) saveImage);
			}
			// When the settings is pressed, temporarily opens the dialog and saves the map to the chosen location.
			else if(mainScreen.settings.isPressed())
			{
				MapSaveImage saveImage = mapEditor.save();
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showSaveDialog(fc);
	            if (returnVal == JFileChooser.APPROVE_OPTION) 
	            {
	                File file = fc.getSelectedFile();
	                String filePath = file.getPath();
					try
					{ 
						FileHandle fileHandle = Gdx.files.local(filePath);
						Json json = new Json();
						//System.out.println(json.prettyPrint(saveImage));
						//fileHandle.writeString(json.prettyPrint(saveImage), false);
						//json.toJson(saveImage, MapEditorSaveImage.class, fileHandle);
						//json.setOutputType(OutputType.minimal);
						json.toJson(saveImage, fileHandle);
					}
					catch(Exception exc)
					{
						exc.printStackTrace(); 
					}
	            }
			}*/
			//When the quit button is pressed, closes the game.
			else if(mainScreen.quit.isPressed())
			{
				Gdx.app.exit();
			}
			else
			{
				
			}
		}
		//Checks whether the player wants to return to the main screen when the map editor screen is the current screen
		else if(this.getScreen() == mapEditor)
		{
			//Sets the main screen as the current screen if the player presses ESC button when the map editor screen is active.
			if(mapEditor.mainMenu)
			{
				this.setScreen(mainScreen);
				mapEditor.mainMenu = false;
			}
			if(mapEditor.isReset)
			{
				mapEditor = new MapEditor();
				this.setScreen(mapEditor);
			}
			//Delete the map editor, then create and set a new one if the player presses D button when the map editor screen is active.
		  /*if(mapEditor.delete)
			{
				mapEditor = new MapEditor();
				setScreen(mapEditor);
			}*/
				
		}
		else
		{
			
		}
		
	}
}
